package com.raven.craft.code.generator;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Collection;

@Slf4j
@EnableConfigurationProperties
@SpringBootApplication
public class CodeGeneratorApplication implements CommandLineRunner, ApplicationContextAware {

    private ApplicationContext applicationContext;

    public static void main(String[] args) {
        SpringApplication.run(CodeGeneratorApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Collection<IGenerator> generators = applicationContext.getBeansOfType(IGenerator.class).values();
        if (generators.isEmpty()) {
            log.info("no file need to generate");
        } else {
            generators.forEach(IGenerator::generate);
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
